<template>
  <div class="custom-input" :class="{ 'custom-input--error': error }">
    <input
      :type="type"
      :value="modelValue"
      :placeholder="placeholder"
      :disabled="disabled"
      :readonly="readonly"
      class="custom-input__field"
      @input="handleInput"
      @focus="handleFocus"
      @blur="handleBlur"
    />
    <div v-if="error" class="custom-input__error">{{ error }}</div>
  </div>
</template>

<script setup lang="ts">
interface Props {
  modelValue?: string
  type?: string
  placeholder?: string
  disabled?: boolean
  readonly?: boolean
  error?: string
}

interface Emits {
  (e: 'update:modelValue', value: string): void
  (e: 'focus'): void
  (e: 'blur'): void
}

const props = withDefaults(defineProps<Props>(), {
  type: 'text',
  placeholder: '',
  disabled: false,
  readonly: false
})

const emit = defineEmits<Emits>()

const handleInput = (event: Event) => {
  const target = event.target as HTMLInputElement
  emit('update:modelValue', target.value)
}

const handleFocus = () => {
  emit('focus')
}

const handleBlur = () => {
  emit('blur')
}
</script>

<style scoped>
.custom-input {
  position: relative;
}

.custom-input__field {
  width: 100%;
  height: 44px;
  padding: 0 16px;
  border: 1px solid #e5e5e5;
  border-radius: 8px;
  font-size: 16px;
  color: #333;
  background-color: #fff;
  transition: all 0.3s ease;
  outline: none;
}

.custom-input__field:focus {
  border-color: #1989fa;
  box-shadow: 0 0 0 2px rgba(25, 137, 250, 0.1);
}

.custom-input__field:disabled {
  background-color: #f5f5f5;
  color: #999;
  cursor: not-allowed;
}

.custom-input__field::placeholder {
  color: #999;
}

.custom-input--error .custom-input__field {
  border-color: #ff4d4f;
}

.custom-input--error .custom-input__field:focus {
  border-color: #ff4d4f;
  box-shadow: 0 0 0 2px rgba(255, 77, 79, 0.1);
}

.custom-input__error {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 4px;
  font-size: 12px;
  color: #ff4d4f;
  line-height: 1.4;
}
</style>
